/**
 * 
 */
package kr.co.insoft.core.service.configration.vender.Impl;

import java.util.ArrayList;
import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.configration.vender.VenderEntityModel;
import kr.co.insoft.core.model.configration.vender.VenderTypeEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.vender.VenderRepository;
import kr.co.insoft.core.service.configration.vender.VenderService;
import kr.co.insoft.driver.model.CloudSponsor;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author skpark
 * 
 */
@Service
public class VenderServiceImpl implements VenderService {

	@Autowired
	VenderRepository venderRepository;

	private static final Logger logger = LoggerFactory
			.getLogger(VenderServiceImpl.class);

	@Override
	public PagingModel getLists(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());

			List<VenderEntityModel> lists = venderRepository
					.getLists(pagingModel);
			int totalCount = venderRepository.getTotalCount(pagingModel);

			logger.info("{}", pagingModel.getSearchAndCondition());

			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public List<VenderEntityModel> getComboList() throws ServiceException {
		VenderEntityModel venderEntityModel = new VenderEntityModel();
		try {

			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			venderEntityModel.setAuth(user);

			List<VenderEntityModel> list = venderRepository
					.getList(venderEntityModel);

			return list;
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public List<VenderTypeEntityModel> getVenderTypeList()
			throws ServiceException {
		try {
			List<VenderTypeEntityModel> result = new ArrayList<VenderTypeEntityModel>();
			VenderTypeEntityModel tmp = null;
			for (CloudSponsor sponsor : CloudSponsor.getPossibleSponsor()) {
				tmp = new VenderTypeEntityModel();
				tmp.setType(sponsor.getCloudName());
				tmp.setTypeSeq(sponsor.ordinal());
				tmp.setTypeDesc(sponsor.getDescription());
				tmp.setTypeName(sponsor.name());
				result.add(tmp);
			}
			return result;
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createVender(VenderEntityModel venderEntityModel)
			throws ServiceException {

		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			venderEntityModel.setAuth((User) auths.getPrincipal());

			/* Vender 등록 */
			venderRepository.createVender(venderEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void syncVenderType() throws ServiceException {
		try {
			venderRepository.deleteVenderType();

			VenderTypeEntityModel tmp = null;
			for (CloudSponsor sponsor : CloudSponsor.getPossibleSponsor()) {
				tmp = new VenderTypeEntityModel();
				tmp.setType(sponsor.getCloudName());
				tmp.setTypeSeq(sponsor.ordinal());
				tmp.setTypeDesc(sponsor.getDescription());
				tmp.setTypeName(sponsor.name());
				venderRepository.addVenderType(tmp);
			}
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public VenderEntityModel getArticle(String venderid)
			throws ServiceException {

		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			VenderEntityModel venderEntityModel = new VenderEntityModel();
			venderEntityModel.setVenderid(venderid);
			venderEntityModel.setAuth((User) auths.getPrincipal());
			return venderRepository.getVender(venderEntityModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public int isExistsVenderName(String venderName) throws ServiceException {

		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			VenderEntityModel venderEntityModel = new VenderEntityModel();
			venderEntityModel.setVenderName(venderName);
			venderEntityModel.setAuth((User) auths.getPrincipal());
			return venderRepository.isExistsVenderName(venderEntityModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void editVender(VenderEntityModel venderEntityModel)
			throws ServiceException {
		try {
			venderRepository.editVender(venderEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void removeVender(VenderEntityModel venderEntityModel)
			throws ServiceException {
		try {
			venderRepository.removeVender(venderEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
}
